package com.itheima.app.service.impl;

import com.itheima.app.mapper.AppointmentMapper;
import com.itheima.app.mapper.MemberMapper;
import com.itheima.app.mapper.OrderMapper;
import com.itheima.app.mapper.SetMealMapper;
import com.itheima.app.service.AppointmentService;
import com.itheima.health.model.entry.Result;
import com.itheima.health.model.pojos.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SetMealMapper setmealMapper;

    @Override
    public Result submitOrder(submitOrderDTO orderDTO) {

        if (!orderDTO.getValidateCode().equals("123456")) {
            return Result.error("验证码错误");
        }
        OrderSetting orderSetting = appointmentMapper.orderSeting(orderDTO.getOrderDate());
        if (orderSetting == null) {
            return Result.error("所选日期未开放预约");
        }
        if (orderSetting.getNumber() == orderSetting.getReservations()) {
            return Result.error("当日预约已满");
        }
        Member member = appointmentMapper.seleteMember(orderDTO.getTelephone());
        if (member == null) {
            member = new Member();
            // BeanUtils.copyProperties(orderDTO, member);
            member.setSex(orderDTO.getSex().toString());
            member.setPhoneNumber(orderDTO.getTelephone());
            member.setIdCard(orderDTO.getIdCard());
            member.setName(orderDTO.getName());
            memberMapper.insert(member);
        }
        Order thisOrder = appointmentMapper.seleteOrder(member.getId(), orderDTO);
        if (thisOrder != null) {
            return Result.error("已经预约过");
        }
        Order build = Order.builder()
                .memberId(member.getId())
                .orderDate(orderDTO.getOrderDate())
                .orderType("微信")
                .setmealId(orderDTO.getSetmealId().intValue())
                .orderStatus("未到诊").build();
        //orderMapper.insert(build);
        //appointmentMapper.checkAppointment(orderDTO.getTelephone())
        appointmentMapper.insertOrder(build);
        appointmentMapper.updateByDate();
        return Result.success(build.getId());
    }

    @Override
    public ReservationInformation findById(Integer id) {
        Order order = appointmentMapper.selectOrderById(id);
        Member member = memberMapper.selectById(order.getMemberId());
        Setmeal setmeal = setmealMapper.selectById(order.getSetmealId().longValue());

        return ReservationInformation.builder()
                .orderDate(order.getOrderDate())
                .setmeal(setmeal.getName())
                .orderType(order.getOrderType())
                .member(member.getName()).build();
    }
}
